﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.hhx;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.Beginning.Api.Application.Command.Farrowing
{
    public class UpddateFarrowingsHandler : IRequestHandler<UpdateFarrowingsCommand, Result>
    {
        private readonly IMapper mapper;
        /// <summary>
        /// 母猪分娩仓储
        /// </summary>
        private readonly IBaseRepository<Farrowings> farrowingsRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="farrowingsRepository"></param>
        public UpddateFarrowingsHandler(IMapper mapper, IBaseRepository<Farrowings> farrowingsRepository)
        {
            this.mapper = mapper;
            this.farrowingsRepository = farrowingsRepository;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(UpdateFarrowingsCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Updated };
            try
            {
                Farrowings farrowings = mapper.Map<Farrowings>(request);
                var res = await farrowingsRepository.Update(farrowings);
                result.Code = (int)CommonErrorCode.Success;
                result.Msg = "母猪分娩修改成功";
            }
            catch (Exception)
            {
                result.Msg = "母猪分娩信息修改异常";
            }
            return result;
        }
    }
}
